package com.toycloud.system.service.impl.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.toycloud.business.dto.params.business.AssociationDemandParams;
import com.toycloud.business.dto.params.business.FeedbackApplicationVersionManagerDetailParams;
import com.toycloud.business.dto.results.CommonResult;
import com.toycloud.business.service.IDemandRecordService;
import com.toycloud.common.core.utils.DateUtils;
import com.toycloud.system.domain.business.FeedbackApplicationVersionManager;
import com.toycloud.system.domain.business.FeedbackApplicationVersionManagerDetail;
import com.toycloud.system.domain.business.FeedbackProductVersionManager;
import com.toycloud.system.domain.business.FeedbackProductVersionManagerDetail;
import com.toycloud.system.mapper.business.FeedbackApplicationVersionManagerDetailMapper;
import com.toycloud.system.service.business.IFeedbackApplicationVersionManagerDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 应用版本管理关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-18
 */
@Service
public class FeedbackApplicationVersionManagerDetailServiceImpl implements IFeedbackApplicationVersionManagerDetailService
{
    @Resource
    private FeedbackApplicationVersionManagerDetailMapper feedbackApplicationVersionManagerDetailMapper;
    @Autowired
    private IDemandRecordService iDemandRecordService;

    /**
     * 查询应用版本管理关联
     * 
     * @param id 应用版本管理关联主键
     * @return 应用版本管理关联
     */
    @Override
    public FeedbackApplicationVersionManagerDetail selectFeedbackApplicationVersionManagerDetailById(Long id)
    {
        return feedbackApplicationVersionManagerDetailMapper.selectFeedbackApplicationVersionManagerDetailById(id);
    }

    /**
     * 查询应用版本管理关联列表
     * 
     * @param feedbackApplicationVersionManagerDetailParams 应用版本管理关联
     * @return 应用版本管理关联
     */
    @Override
    public CommonResult<List<FeedbackApplicationVersionManagerDetail>> selectFeedbackApplicationVersionManagerDetailList(FeedbackApplicationVersionManagerDetailParams feedbackApplicationVersionManagerDetailParams)
    {
        PageHelper.startPage(feedbackApplicationVersionManagerDetailParams.getCurrent(), feedbackApplicationVersionManagerDetailParams.getPageSize());
        List<FeedbackApplicationVersionManagerDetail> feedbackApplicationVersionDetailManagersDetail = feedbackApplicationVersionManagerDetailMapper.selectFeedbackApplicationVersionManagerDetailList(feedbackApplicationVersionManagerDetailParams);
        Page<FeedbackApplicationVersionManagerDetail> feedbackApplicationVersionManagersDetailPage = (Page<FeedbackApplicationVersionManagerDetail>) feedbackApplicationVersionDetailManagersDetail;
        return CommonResult.Success((int) feedbackApplicationVersionManagersDetailPage.getTotal(), feedbackApplicationVersionDetailManagersDetail);

    }

    @Override
    public CommonResult<List<Map<String, Object>>> listDetail(String ids) {
        //遍历出所有的id
        String[] split = ids.split(",");
        List<Map<String, Object>> res = new ArrayList<>();
            List<FeedbackApplicationVersionManagerDetail> feedbackApplicationVersionManagers = feedbackApplicationVersionManagerDetailMapper.listDetail(split);
            List<String> parentids = feedbackApplicationVersionManagers.stream().map(x -> x.getParentid()).collect(Collectors.toList());
            List<FeedbackApplicationVersionManager> feedbackApplicationVersionManagers1 = feedbackApplicationVersionManagerDetailMapper.listDetail2(parentids);
        List<String> appids = feedbackApplicationVersionManagers1.stream().map(item -> item.getId()).collect(Collectors.toList());
        List<FeedbackProductVersionManagerDetail> appversiondList = feedbackApplicationVersionManagerDetailMapper.listDetail3(appids);
        List<String> pids = appversiondList.stream().map(x -> x.getParentid()).collect(Collectors.toList());
        List<FeedbackProductVersionManager> appversionList = new ArrayList<>();
        if(pids.isEmpty()){
            appversionList = new ArrayList<>();
        }else{
            appversionList = feedbackApplicationVersionManagerDetailMapper.listDetail4(pids);
        }

        for (String s : split){
            Map<String, Object> map = new HashMap<>();
            List<FeedbackApplicationVersionManagerDetail> res1 = feedbackApplicationVersionManagers.stream().filter(item -> item.getDemandid().equals(s)).collect(Collectors.toList());
            List<String> pid = res1.stream().map(x -> x.getParentid()).collect(Collectors.toList());
            List<FeedbackApplicationVersionManager> res2 = feedbackApplicationVersionManagers1.stream().filter(item -> pid.contains(item.getId())).collect(Collectors.toList());
            List<String> pid2 = res2.stream().map(x -> x.getId()).collect(Collectors.toList());
            List<FeedbackProductVersionManagerDetail> res3 = appversiondList.stream().filter(item -> pid2.contains(item.getApplicationid())).collect(Collectors.toList());
            List<String> pid3 = res3.stream().map(x -> x.getParentid()).collect(Collectors.toList());
            List<FeedbackProductVersionManager> res4 = appversionList.stream().filter(item -> pid3.contains(item.getId())).collect(Collectors.toList());
            map.put("applicationids", res2.stream().map(x -> x.getApplicationid()).collect(Collectors.toList()));
            map.put("applicationversions", res2.stream().map(x -> x.getApplicationversion()).collect(Collectors.toList()));
            map.put("producttypeids", res4.stream().map(x -> x.getProducttypeid()).collect(Collectors.toList()));
            map.put("productmodelids", res4.stream().map(x -> x.getProductmodelid()).collect(Collectors.toList()));
            map.put("modelversions", res4.stream().map(x -> x.getModelversion()).collect(Collectors.toList()));
            res.add(map);
        }
        return CommonResult.Success(res);
    }

    /**
     * 新增应用版本管理关联
     * 
     * @param feedbackApplicationVersionManagerDetail 应用版本管理关联
     * @return 结果
     */
    @Override
    public int insertFeedbackApplicationVersionManagerDetail(FeedbackApplicationVersionManagerDetail feedbackApplicationVersionManagerDetail)
    {
        feedbackApplicationVersionManagerDetail.setCreateTime(DateUtils.getNowDate());
        return feedbackApplicationVersionManagerDetailMapper.insertFeedbackApplicationVersionManagerDetail(feedbackApplicationVersionManagerDetail);
    }

    /**
     * 修改应用版本管理关联
     * 
     * @param feedbackApplicationVersionManagerDetail 应用版本管理关联
     * @return 结果
     */
    @Override
    public int updateFeedbackApplicationVersionManagerDetail(FeedbackApplicationVersionManagerDetail feedbackApplicationVersionManagerDetail)
    {
        feedbackApplicationVersionManagerDetail.setUpdateTime(DateUtils.getNowDate());
        return feedbackApplicationVersionManagerDetailMapper.updateFeedbackApplicationVersionManagerDetail(feedbackApplicationVersionManagerDetail);
    }

    /**
     * 批量删除应用版本管理关联
     * 
     * @param ids 需要删除的应用版本管理关联主键
     * @return 结果
     */
    @Override
    public int deleteFeedbackApplicationVersionManagerDetailByIds(Long[] ids)
    {
        return feedbackApplicationVersionManagerDetailMapper.deleteFeedbackApplicationVersionManagerDetailByIds(ids);
    }

    /**
     * 删除应用版本管理关联信息
     * 
     * @param id 应用版本管理关联主键
     * @return 结果
     */
    @Override
    public int deleteFeedbackApplicationVersionManagerDetailById(String id)
    {
        return feedbackApplicationVersionManagerDetailMapper.deleteFeedbackApplicationVersionManagerDetailById(id);
    }
    @Override
    public int deleteFeedbackApplicationVersionManagerDetailByParentId(String id)
    {
        int i = feedbackApplicationVersionManagerDetailMapper.deleteFeedbackApplicationVersionManagerDetailByParentId(id);
        List<AssociationDemandParams> demandsByParentId = feedbackApplicationVersionManagerDetailMapper.getDemandsByParentId(id);
        iDemandRecordService.updateVersion(String.join(",", demandsByParentId.stream().map(item->item.getId()).collect(Collectors.toList())));
        return i;
    }
}
